// __PUBLIC__/js/page/admin/content/weibo
require(['../../common'], function() {
    require(['content.weibo'])
})

define('content.weibo', [
    'jquery',
    'page/grid'
], function($, Grid) {
    'use strict';

    var $table = $('.table')
    var mygrid = new Grid($table)
    mygrid.init()

    /*function clickCheckbox(objChk, objTxt) {
     if (objChk.checked)
     document.getElementById(objTxt).value = 'Y';
     else
     document.getElementById(objTxt).value = 'N';
     }
     //只能输入数字
     
     function KeyCodeOnlyNumber() {
     if (window.event.keyCode == 37 || window.event.keyCode == 38 || window.event.keyCode == 39 || window.event.keyCode == 40) {
     return;
     }
     if (!((window.event.keyCode >= 48 && window.event.keyCode <= 57) || (window.event.keyCode >= 96 && window.event.keyCode <= 105) || window.event.keyCode == 13 || window.event.keyCode == 8 || window.event.keyCode == 46 || window.event.keyCode == 9)) {
     window.event.returnValue = false;
     }
     }*/
    $(document)
            .on('click', '.A_screen', function(e) {
                e.preventDefault()
                deleteWeibo(1, getASelected(this))
            })
            .on('click', '.A_del', function(e) {
                e.preventDefault()
                deleteWeibo(2, getASelected(this))
            })
            .on('click', '.recover', function(e) {
                e.preventDefault()
                deleteWeibo(0, getASelected(this))
            })
            .on('click', '#J_screen', function(e) {
                e.preventDefault()
                var selected = getSelected()
                deleteWeibo(1, selected)
            })
            .on('click', '#J_restore', function(e) {
                e.preventDefault()
                var selected = getSelected()
                deleteWeibo(0, selected)
            })
            .on('click', '#J_del', function(e) {
                e.preventDefault()
                var selected = getSelected()
                deleteWeibo(2, selected)
            })
            .on('click', '#J_search', function(e) {
                e.preventDefault()
                sureSearchAttach()
            })
            .on('click', '.A_recommend', function(e) {
                e.preventDefault()
                if(recommendWeibo(getASelected(this))){
                	$(this).text('取消推荐');
                	$(this).attr('class','A_cancelrecommend')
                	$(this).css('color','red')
                }
            })
            .on('click', '.A_cancelrecommend', function(e) {
                e.preventDefault()
                if(cancelRecommendWeibo(getASelected(this))){
                	$(this).text('热门推荐');
                	$(this).attr('class','A_recommend')
                	$(this).css('color','')
                }
            })

    function getSelected() {
        var selected = mygrid.getSelected()
        if (!selected.count) {
            alert(lang.pleaseSelectAData)
            return
        }
        return selected
    }

    function getASelected(t) {
        var current = mygrid.getCurrent(t)
        var selected = {
            id: [current.id],
            index: [current.index]
        }
        return selected
    }

    //isdel 0- 恢复 1-屏蔽 2-彻底删除

    function deleteWeibo(isdel, selected) {
        if (confirm('确定操作？')) {
            $.post(APP + '?app=admin&m=Content&a=delWeibo', {
                ids: selected.id.join(),
                isdel: isdel
            }).done(function(res) {
                if (res == '1') {
                    switch (isdel) {
                        case 0:
                            alert(lang.recoverSuccess);
                            break;
                        case 1://同样的提示
                        case 2:
                            alert(lang.delSuccess);
                            break;
                    }
                    mygrid.del(selected.index)
                    sureSearchAttach()
                } else {
                    switch (isdel) {
                        case 0:
                            alert(lang.recoverFail);
                            break;
                        case 1://同样的提示
                        case 2:
                            alert(lang.delFails);
                            break;
                    }
                }
            })
        }
    }
    
    function recommendWeibo(selected) {   
    	var isSuc = false
        $.ajax({
    		url : APP + '?app=admin&m=Content&a=doRecommendWeibo',
    		async : false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
    		type : "POST",
    		dataType : "json",
    		data:"ids=" + selected.id.join(),
    		success : function(res) {
    			if (res == '1') {           
	              isSuc =  true
	            } else {
	              alert(lang.fails);
	            }
    		}
    	})
    	return isSuc
    }
    
    function cancelRecommendWeibo(selected) {
    	var isSuc = false
        $.ajax({
    		url : APP + '?app=admin&m=Content&a=cancelRecommendWeibo',
    		async : false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
    		type : "POST",
    		dataType : "json",
    		data:"ids=" + selected.id.join(),
    		success : function(res) {
    			if (res == '1') {           
	              isSuc =  true
	            } else {
	              alert(lang.fails);
	            }
    		}
    	})
    	return isSuc
    }
    
    function sureSearchAttach() {
        var weibo_id = $('#weibo_id').val()
        var recycle = $('#recycle').val()
        var uid = $('#uid').val()
        var content = $('#content').val()
        var is_recommend = $('#is_recommend').val()
        var type = $('#type').val()
        var str_get = '&weibo_id=' + weibo_id + '&recycle=' + recycle + '&uid=' + uid + '&content=' + content + '&is_recommend=' + is_recommend + '&type=' + type
        location.href = APP + '?app=admin&m=Content&a=weibo' + str_get
    }
})
